import request from '../utils/request';
import type { MenuListType } from '@/types/menu';

// 统一的API响应类型
interface ApiResponse<T = any> {
    msg: string;
    timeStamp: number;
    code: number;
    data?: T;
}

/**
 * 获取用户菜单列表
 * @returns Promise，返回标准化响应对象
 */
export const userMenuAPI = async (): Promise<{
    success: boolean;
    message: string;
    data: MenuListType[] | null;
}> => {
    try {
        // 获取API响应
        const res = await request.get<ApiResponse<MenuListType[]>>('/menu/getUserMenu');

        if (res.code === 200 && res.data) {
            return {
                success: true,
                message: res.msg || '获取菜单成功',
                data: res.data
            };
        }

        return {
            success: false,
            message: res.msg || '菜单数据格式错误',
            data: null
        };

    } catch (error: any) {
        console.error('获取用户菜单失败:', error);

        const errorMessage = error.response?.data?.msg
            || error.message
            || '请求菜单数据时发生未知错误';

        return {
            success: false,
            message: errorMessage,
            data: null
        };
    }
};

export default {
    userMenuAPI
};